package com.tom.springframework.context.support;

import com.tom.springframework.beans.BeansException;
import com.tom.springframework.beans.factory.ConfigurableListableBeanFactory;
import com.tom.springframework.beans.factory.support.DefaultListableBeanFactory;

/**
 * 抽象基类刷新应用上下文 Base class for {@link com.tom.springframework.context.ApplicationContext}
 * implementations which are supposed to support multiple calls to {@link #refresh()},
 * creating a new internal bean factory instance every time.
 * Typically (but not necessarily), such a context will be driven by
 * a set of config locations to load bean definitions from.
 *
 * <pre>
 *     使用refreshBeanFactory 抽象方法可以获取DefaultListableBeanFactory的实例化,以及对资源配置的加载
 *     loadBeanDefinitions(beanFactory),在加载完成后即可完成对spring.xml配置文件中的Bean对象的定义和注册
 *     ,也实现了BeanFactoryPostProcessor接口、BeanPostProcess接口对Bean信息的配置。
 * </pre>
 * @author zx
 * @date 2024-11-24 16:45
 */
public abstract class AbstractRefreshableApplicationContext extends AbstractApplicationContext {
    private DefaultListableBeanFactory beanFactory;

    @Override
    protected void refreshBeanFactory() throws BeansException {
        DefaultListableBeanFactory beanFactory = createBeanFactory();
        loadBeanDefinitions(beanFactory);
        this.beanFactory = beanFactory;
    }

    private DefaultListableBeanFactory createBeanFactory() {
        return new DefaultListableBeanFactory();
    }

    protected abstract void loadBeanDefinitions(DefaultListableBeanFactory beanFactory);

    @Override
    protected ConfigurableListableBeanFactory getBeanFactory() {
        return beanFactory;
    }

}
